package LeetCode.month02.day37;

import java.io.InputStream;
import java.util.Arrays;

public class No3459 {

    public static void main(String[] args) {
        int[][] m = new int[][]{{2,2,1,2,2},{2,0,2,2,0},{2,0,1,1,0},{1,0,2,2,2},{2,0,0,2,2}};
        for (int[] ints : m) {
            System.out.println(Arrays.toString(ints));
        }
    }
    static int[][] DR = {{1,1},{1,-1},{-1,-1},{-1,1}};
    public static int lenOfVDiagonal(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][][] mx = new int[m][n][1<<3];
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(grid[i][j]==1){
                    for (int k = 0; k < 4; k++) {
                        res = Math.max(res,dfs(i,j,k,grid,mx,1,2)+1);
                    }
                }
            }
        }
        return res;
    }

    private static int dfs(int i, int j, int k, int[][] grid, int[][][] mx, int canTurn,int target) {
        i+=DR[k][0];
        j+=DR[k][1];
        if(i<0||i>=grid.length||j<0||j>=grid[0].length||grid[i][j]!=target) return 0;
        int index = k<<1|canTurn;
        if(mx[i][j][index]>0) return mx[i][j][index];
        int res = dfs(i,j,k,grid,mx,canTurn,2-target)+1;
        if(canTurn==1){
            res = Math.max(dfs(i,j,(k+1)%4,grid,mx,0,2-target)+1,res);
        }
        return mx[i][j][index] = res;
    }
}
